package com.ruoyi.coal.modules.coalpile.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.Map;

import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.Query;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CutUpEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CutUPCircleVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.CutUpVo;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardInfoDao;
import com.ruoyi.coal.modules.coalpile.mapper.CutUpDao;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import com.ruoyi.coal.modules.coalpile.service.CutUpService;
import com.ruoyi.coal.modules.coalpile.service.PointService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("cutUpService")
public class CutUpServiceImpl extends ServiceImpl<CutUpDao, CutUpEntity> implements CutUpService {
    @Autowired
    PointService pointService;

    @Autowired
    CoalYardInfoService coalYardInfoService;

    @Autowired
    CoalYardInfoDao coalYardInfoDao;

    @Autowired
    CutUpDao cutUpDao;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CutUpEntity> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty((String)params.get("key")))
            wrapper.eq("coal_yard_id", params.get("key"));
        IPage<CutUpEntity> page = page((new Query())
                .getPage(params), (Wrapper)((QueryWrapper)wrapper
                .eq("measure_type", Integer.valueOf(1))).orderBy(true, false, (Object[])new String[] { "create_time" }));
        return new PageUtils(page);
    }

    @Transactional
    public void saveCutUp(CutUpVo cutUp) {
        this.pointService.save(cutUp.getXa1());
        this.pointService.save(cutUp.getXa2());
        this.pointService.save(cutUp.getXb1());
        this.pointService.save(cutUp.getXb2());
        this.pointService.save(cutUp.getYa1());
        this.pointService.save(cutUp.getYa2());
        this.pointService.save(cutUp.getYb1());
        this.pointService.save(cutUp.getYb2());
        CutUpEntity cutUpEntity = new CutUpEntity();
        if (cutUp.getCoalYardId() == null) {
            CoalYardInfoEntity coalYardInfoEntity = (CoalYardInfoEntity)this.coalYardInfoDao.selectOne((Wrapper)(new QueryWrapper()).eq("name", "导入料堆"));
                    cutUpEntity.setCoalYardId(coalYardInfoEntity.getId());
        } else {
            cutUpEntity.setCoalYardId(cutUp.getCoalYardId());
        }
        cutUpEntity.setName(cutUp.getName());
        cutUpEntity.setXa1(cutUp.getXa1().getId());
        cutUpEntity.setXa2(cutUp.getXa2().getId());
        cutUpEntity.setXb1(cutUp.getXb1().getId());
        cutUpEntity.setXb2(cutUp.getXb2().getId());
        cutUpEntity.setYa1(cutUp.getYa1().getId());
        cutUpEntity.setYa2(cutUp.getYa2().getId());
        cutUpEntity.setYb1(cutUp.getYb1().getId());
        cutUpEntity.setYb2(cutUp.getYb2().getId());
        cutUpEntity.setCoalPileVariety(cutUp.getCoalPileVariety());
        cutUpEntity.setMeasureType(cutUp.getMeasureType());
        if (cutUp.getCoalPileDensity() != null) {
            cutUpEntity.setCoalPileDensity(cutUp.getCoalPileDensity());
        } else {
            cutUpEntity.setCoalPileDensity(Double.valueOf(0.0D));
        }
        if (cutUp.getCoalPileGround() != null) {
            cutUpEntity.setCoalPileGround(cutUp.getCoalPileGround());
        } else {
            cutUpEntity.setCoalPileGround(Double.valueOf(0.0D));
        }
        cutUpEntity.setCutType(Integer.valueOf(0));
        save(cutUpEntity);
    }

    public void saveCircleCutUp(CutUPCircleVo cutUp) {
        CutUpEntity cutUpEntity = new CutUpEntity();
        if (cutUp.getCoalYardId() == null) {
            CoalYardInfoEntity coalYardInfoEntity = (CoalYardInfoEntity)this.coalYardInfoDao.selectOne((Wrapper)(new QueryWrapper()).eq("name", "导入料堆"));
                    cutUpEntity.setCoalYardId(coalYardInfoEntity.getId());
        } else {
            cutUpEntity.setCoalYardId(cutUp.getCoalYardId());
        }
        cutUpEntity.setName(cutUp.getName());
        cutUpEntity.setXa1(cutUp.getStartAngle());
        cutUpEntity.setXa2(cutUp.getStopAngle());
        cutUpEntity.setMeasureType(cutUp.getMeasureType());
        CoalYardInfoEntity coalYard = (CoalYardInfoEntity)this.coalYardInfoService.getById(cutUp.getCoalYardId());
        if (cutUp.getCoalPileDensity() != null) {
            cutUpEntity.setCoalPileDensity(cutUp.getCoalPileDensity());
        } else {
            cutUpEntity.setCoalPileDensity(coalYard.getDensity());
        }
        if (cutUp.getCoalPileGround() != null) {
            cutUpEntity.setCoalPileGround(cutUp.getCoalPileGround());
        } else {
            cutUpEntity.setCoalPileGround(coalYard.getDensity());
        }
        cutUpEntity.setCoalPileVariety(cutUp.getCoalPileVariety());
        cutUpEntity.setCutType(Integer.valueOf(1));
        save(cutUpEntity);
    }

    public CutUpEntity selectCutUpAll(Integer id) {
        return this.cutUpDao.selectCutUpAll(id);
    }

    public int queryCutUp(Integer id) {
        return this.cutUpDao.cutUpNum(id);
    }

    public List<String> cutUpnames(Integer id) {
        return this.cutUpDao.cutUpNames(id);
    }

    public CutUpEntity getCutUpByCutUpId(Integer id) {
        return this.cutUpDao.getCutUpByCutUpId(id);
    }
}

